import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_statusbar_manager/flutter_statusbar_manager.dart';
import 'package:shop/http/HttpUtil.dart';

class MyRoute {

  static Function popToSelectPage ;

  @optionalTypeArgs
  static void pop<T extends Object>([ T result ]){
    FlutterStatusbarManager.setStyle(StatusBarStyle.DARK_CONTENT) ;
    navigatorKey.currentState.pop(result) ;
  }

  static Future<T> push<T extends Object>(Widget widget , [bool shouldPop,String page]){
    FlutterStatusbarManager.setStyle(StatusBarStyle.DARK_CONTENT) ;
    if(shouldPop != null && shouldPop) {
      //String page = navigatorKey.currentWidget.runtimeType.toString()  ;
      popToSelectPage = (){
        popUntil(page) ;
        popToSelectPage = null ;
      } ;
    }
    return navigatorKey.currentState.push(MaterialPageRoute(builder: (ctx){
      return widget ;
    } , settings: RouteSettings(name: widget.runtimeType.toString()))) ;
  }

  static Future<T> pushNamedAndRemoveUntil<T extends Object>(
      String newRouteName,
      RoutePredicate predicate, {
        Object arguments,
      }){
    return navigatorKey.currentState.pushNamedAndRemoveUntil(newRouteName , predicate) ;
  }

  static void popUntil(String settings){ // "/" Chat
    navigatorKey.currentState.popUntil((route){
      if (route.settings.name == settings){
        return true;
      }else {
        return false;
      }
    }) ;
  }

  static replace<T extends Object>(Widget widget){
    navigatorKey.currentState.pushReplacement(MaterialPageRoute(builder: (ctx){
      return widget ;
    })) ;
  }
}